package com.klikmee.sms.core.base.communicator.vip;

import java.text.MessageFormat;
import java.util.concurrent.ConcurrentMap;

import org.apache.log4j.Logger;

import com.klikmee.sms.core.base.CoreCacheDriver;
import com.klikmee.sms.core.base.communicator.DeliveryReport;
import com.klikmee.sms.core.base.util.Constants.Loggers;
import com.klikmee.sms.core.model.OutgoingMessage;

public class VipChargedCacheDriverImpl implements CoreCacheDriver<DeliveryReport, OutgoingMessage> {

	
	/**
	 * Logger instance
	 */
	public Logger logger = Logger.getLogger( Loggers.CORE_LOGGER );
	
	/**
	 * Charged cache reference
	 */
	private ConcurrentMap<String, OutgoingMessage>	chargedCache;
	
	@Override
	public void write(OutgoingMessage object) {
		chargedCache.put( object.getProvidersMesssageId(), object);
		logger.info( MessageFormat.format( "OutgoingMessage [{0}] written to cache", object.getId() ) );
	}

	@Override
	public OutgoingMessage read(DeliveryReport key) {
		OutgoingMessage outgoingMessage = chargedCache.get( key.getProvidersMessageId() );
		if (outgoingMessage != null) {
			logger.debug( MessageFormat.format( "Outgoing message with providers id [{0}] read from cache", outgoingMessage.getProvidersMesssageId() ) );
		}
		return outgoingMessage;
	}

	@Override
	public void remove(OutgoingMessage object) {
		OutgoingMessage outgoingMessage = chargedCache.remove( object.getProvidersMesssageId() );
		logger.debug( MessageFormat.format( "Outgoing message with providers id [{0}] removed from cache", outgoingMessage.getProvidersMesssageId() ) );
	}

	
	public void setChargedCache(ConcurrentMap<String, OutgoingMessage> chargedCache) {
		this.chargedCache = chargedCache;
	}

}
